#include "lvgl_photo_brower.h"

// 提取公共代码为函数
static void Create_Navigation_Buttons(lv_obj_t *main_screen, void *user_data) {
    // 上一页按钮
    lv_obj_t *prev_btn = lv_btn_create(main_screen);
    lv_obj_set_size(prev_btn, 50, 50);
    lv_obj_set_pos(prev_btn, 740, 70);
 
    lv_obj_t *prev_label = lv_label_create(prev_btn);
    lv_label_set_text(prev_label, "Prev");
    lv_obj_align(prev_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(prev_btn, Show_Dir_List, LV_EVENT_CLICKED, user_data);
 
    // 下一页按钮
    lv_obj_t *next_btn = lv_btn_create(main_screen);
    lv_obj_set_size(next_btn, 50, 50);
    lv_obj_set_pos(next_btn, 740, 130);
 
    lv_obj_t *next_label = lv_label_create(next_btn);
    lv_label_set_text(next_label, "Next");
    lv_obj_align(next_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(next_btn, next_page_event_handler, LV_EVENT_CLICKED, user_data);

    // 退出按钮
    lv_obj_t *exit_btn = lv_btn_create(main_screen);
    lv_obj_set_size(exit_btn, 50, 50);
    lv_obj_set_pos(exit_btn, 740, 10);
    lv_obj_set_style_bg_color(exit_btn, lv_color_hex(0xFF0000), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(exit_btn, LV_OPA_90, LV_PART_MAIN);
    lv_obj_set_style_radius(exit_btn, 25, LV_PART_MAIN);

    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, "EXIT");
    lv_obj_align(exit_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(exit_btn, exit_btn_event_handler, LV_EVENT_CLICKED, ((SI_P)user_data)->dip);


}

// 修改后的Process_Directory_Entries函数
static bool Process_Directory_Entries(DIR *dp, SI_P si_data, int start_index, int count) {
    int file_btn_x = 10;
    int file_btn_y = 10;
    si_data->btn_nums = 0;

    // 清空按钮链表
    Destroy_Dir_Btn_List(si_data->dip->dir_btn_inf_list_head);

    struct dirent *entry;
    int current_index = 0;
    
    while ((entry = readdir(dp)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

        if (entry->d_type == 8) { // DT_REG == 8
            // 只在指定的索引范围内处理条目
            if (current_index >= start_index && (count == 0 || current_index < start_index + count)) {
                BTN_LINK btn_inf_node = Create_Dir_Btn_Node();
                if (btn_inf_node == (BTN_LINK)-1) {
                    printf("创建目录按钮信息节点失败！\n");
                    closedir(dp);
                    return false;
                }

                btn_inf_node->dip = si_data->dip;
                if (!Add_Dir_Btn_Node(si_data->dip, btn_inf_node)) {
                    printf("头插失败！\n");
                    closedir(dp);
                    return false;
                }

                strcpy(btn_inf_node->file_name, entry->d_name);
                // printf("%s\n", btn_inf_node->file_name);

                if (!Show_File_Btn(btn_inf_node, file_btn_x, file_btn_y)) {
                    printf("显示文件按钮失败！\n");
                    closedir(dp);
                    return false;
                }

                file_btn_x += 210;
                if (file_btn_x >= 640) {
                    file_btn_x = 10;
                    file_btn_y += 150;
                }

                si_data->btn_nums++;
            }
            current_index++;
        }
    }
    return true;
}

void Show_Dir_List(lv_event_t *e) {
    SI_P si_data = (SI_P)e->user_data;
    lv_obj_clean(si_data->dip->main_screen);

    DIR *dp = opendir(si_data->obj_dir_path);
    if (dp == NULL) {
        perror("opendir failed");
        return;
    }

    // 显示前9个按钮
    if (!Process_Directory_Entries(dp, si_data, 0, 9)) {
        closedir(dp);
        return;
    }

    Create_Navigation_Buttons(si_data->dip->main_screen, si_data);

    if (closedir(dp) == -1) {
        perror("closedir failed");
    }
}




void next_page_event_handler(lv_event_t *e) {
    SI_P si_data = (SI_P)e->user_data;
    lv_obj_clean(si_data->dip->main_screen);

    DIR *dp = opendir(si_data->obj_dir_path);
    if (dp == NULL) {
        perror("opendir failed");
        return;
    }

    // 计算起始索引（第10个按钮）
    int start_index = 9; // 0-based索引，第10个按钮是索引9
    
    // 显示从第10个开始的按钮
    if (!Process_Directory_Entries(dp, si_data, start_index, 0)) { // 0表示显示所有剩余按钮
        closedir(dp);
        return;
    }

    Create_Navigation_Buttons(si_data->dip->main_screen, si_data);

    if (closedir(dp) == -1) {
        perror("closedir failed");
    }
}

void prev_page_event_handler(lv_event_t *e) {
    Show_Dir_List(e); // 直接调用Show_Dir_List显示前9个按钮
}

void File_Btn_Del_Push_Task(lv_event_t * e)
{
    BTN_LINK free_node = (BTN_LINK)e->user_data;

    BTN_LINK tmp_node = free_node->prev;
    printf("%s\n",free_node->file_name);

    tmp_node->next = free_node->next;
    free_node->next->prev = tmp_node;
    free_node->next = free_node;
    free_node->prev = free_node;

    // free_node->file_name
    char buffer[32];
    memset(buffer,0,sizeof(buffer));
    sprintf(buffer, "%s%s", "/pic/", free_node->file_name);
    // 删除文件
    if (remove(buffer) == 0) {
        printf("文件 %s 删除成功\n", buffer);
    } else {
        perror("删除文件失败");
    }


    free(free_node);
    
}

// 进度条显示函数
void Show_Progress_Bar(DI_P dip) {
    static lv_style_t style_bg;
    static lv_style_t style_indic;
 
    lv_style_init(&style_bg);
    lv_style_set_border_color(&style_bg, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_border_width(&style_bg, 2);
    lv_style_set_pad_all(&style_bg, 6);
    lv_style_set_radius(&style_bg, 6);
    lv_style_set_anim_time(&style_bg, 1000);
 
    lv_style_init(&style_indic);
    lv_style_set_bg_opa(&style_indic, LV_OPA_COVER);
    lv_style_set_bg_color(&style_indic, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_radius(&style_indic, 3);
 
    lv_obj_t *bar = lv_bar_create(dip->main_screen);
    lv_obj_remove_style_all(bar);
    lv_obj_add_style(bar, &style_bg, 0);
    lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR);
 
    lv_obj_set_size(bar, 200, 20);
    lv_obj_center(bar);
    
    // 添加一个标签来显示进度信息
    lv_obj_t *label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "Loading...");
    lv_obj_align_to(label, bar, LV_ALIGN_OUT_TOP_MID, 0, -10);
    
    // 立即刷新显示
    lv_refr_now(lv_disp_get_default());
    
    // 模拟进度更新
    for (int i = 0; i <= 100; i += 15) {
        lv_bar_set_value(bar, 100, LV_ANIM_ON);
        lv_task_handler();  // 处理LVGL任务
        usleep(200000);     // 200毫秒延迟，确保有足够时间观察
    }
    
    // 确保进度条动画完成
    lv_task_handler();
}

bool Demo_Running(DI_P dip) {

    // 显示进度条
    Show_Progress_Bar(dip);
    // 显示临时目录列表
    
    printf("例程运行成功！\n");

    // sleep(3);
    
    if (Show_Main_Screen(dip,DEFULT_DIR_PATH) == false) {
        printf("显示目录列表失败！\n");
        return false;
    }
    
    return true;
}


void exit_btn_event_handler_main(lv_event_t * e)
{
    lv_obj_clean(((DI_P)e->user_data)->main_screen);
    lv_obj_t * img_obj = lv_img_create(((DI_P)e->user_data)->main_screen);  // 在文件屏幕上创建一个图片对象
    lv_img_set_src(img_obj, "S:/pic/the_end.png");  // 加载静态图片
    lv_obj_center(img_obj);
}


void game_ui(lv_event_t * e)
{
    printf("Game Loading\n");
    
    lv_obj_clean(((DI_P)e->user_data)->main_screen);
    DI_P dip = ((DI_P)e->user_data);
    snake_game_init(dip);
}

